﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace SPA_Prototype.Model
{
    class PersonelManagement
    {
        private DatabaseDataSet _Dataset;
        private DatabaseDataSet.PersonelTableDataTable _DataTable;
        private DatabaseDataSetTableAdapters.PersonelTableTableAdapter _Adt;

        public PersonelManagement(DatabaseDataSet dt)
        {
            _Dataset = dt;
            _DataTable = _Dataset.PersonelTable;
            _Adt = new DatabaseDataSetTableAdapters.PersonelTableTableAdapter();

            LoadDB();
        }

        // medthod : update and load
        public string GetNameOfStaff(int ID)
        {
            string Point = "";
            var query = from q in _DataTable
                        where q.ID == ID
                        select new { q.Name };

            foreach (var order in query) Point = order.Name;
            return Point;
        }

        public void AddPointForStaff(int Id,int points)
        {
            int count = _DataTable.Rows.Count;
            for (int j = 0; j < count; j++)
            {
                if (_DataTable[j].ID == Id)
                {
                    if (_DataTable[j].PointOfMonth == null) _DataTable[j].PointOfMonth = 0;
                    if (_DataTable[j].PointOfYear == null) _DataTable[j].PointOfYear = 0;

                    _DataTable[j].PointOfMonth += points;
                    _DataTable[j].PointOfYear += points;

                    UpdateDB();
                    return;
                }
            }

        }

        public void ResetMonthAcc()
        {
            int count = _DataTable.Rows.Count;
            for (int j = 0; j < count; j++)
            {
                _DataTable[j].PointOfMonth = 0;
            }
            UpdateDB();
        }

        public void ResetYearAcc()
        {
            int count = _DataTable.Rows.Count;
            for (int j = 0; j < count; j++)
            {
                _DataTable[j].PointOfYear = 0;
            }
            UpdateDB();
        }

        public void AddNewStaff(int ID, string name, string NationalID, string Phone, string Position)
        {
            try
            {
                _DataTable.AddPersonelTableRow(ID, name, Position, NationalID, Phone, 0, 0);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Input value is invalid ! \r\n\r\n" + ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }
            UpdateDB();
        }

        private void LoadDB()
        {
            _Adt.Fill(_DataTable);
        }
        
        public void UpdateDB()
        {
            try
            {
                _Adt.Update(_DataTable);
                MessageBox.Show("Successful !", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
